package com.xidian.zhiancloud.service.workTicket;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.dao.workTicket.ConfinedSpaceMapper;
import com.xidian.zhiancloud.dao.workTicket.HighMapper;
import com.xidian.zhiancloud.dto.workTicket.ConfinedSpaceTicketVODTO;
import com.xidian.zhiancloud.dto.workTicket.HighTicketVODTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.workTicket.ConfinedSpaceTicket;
import com.xidian.zhiancloud.model.workTicket.HighTicket;
import com.xidian.zhiancloud.qo.workTicket.ConfinedSpaceTicketQO;
import com.xidian.zhiancloud.qo.workTicket.HighTicketQO;
import com.xidian.zhiancloud.vo.workTicket.ConfinedSpaceTicketVO;
import com.xidian.zhiancloud.vo.workTicket.HighTicketVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class ConfinedSpaceService extends ServiceImpl<ConfinedSpaceMapper, ConfinedSpaceTicket> implements IService<ConfinedSpaceTicket> {
    @Autowired
    private ConfinedSpaceMapper confinedSpaceMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private ConfinedFlowService confinedFlowService;


    /**
     * 根据ConfinedSpaceTicket的id生成VO对象
     *
     * @param companyId
     * @param id
     * @return
     */
    public ConfinedSpaceTicketVODTO getConfinedSpaceTicketVODTOById(Long companyId, Long id) {
        ConfinedSpaceTicketVODTO confinedSpaceTicketVODTO = new ConfinedSpaceTicketVODTO();
        // 根据id获取t_confined_space中的数据项
        ConfinedSpaceTicketVO confinedSpaceTicketVO = new ConfinedSpaceTicketVO();
        ConfinedSpaceTicket confinedSpaceTicket = confinedSpaceMapper.selectById(id);
        if (confinedSpaceTicket == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        BeanUtils.copyProperties(confinedSpaceTicket, confinedSpaceTicketVO);
        // 查找部门
        Organization organization = organizationMapper.selectById(confinedSpaceTicket.getApplicantUnitId());
        confinedSpaceTicketVO.setApplicantUnitName(organization == null ? "该部门不存在" : organization.getName());
        // 处理关联作业票
        String relateWorkNum_s = confinedSpaceTicketVO.getRelateWorkNum();
        if (relateWorkNum_s != null) {
            List<String> relateWorkNums = new LinkedList<>();
            for (String relateWorkNum : relateWorkNum_s.split(",")) {
                relateWorkNums.add(relateWorkNum);
            }
            confinedSpaceTicketVO.setRelateWorkNumList(relateWorkNums);
        }
        // 作业人签字
        String workerSign_s = confinedSpaceTicketVO.getWorkersName();
        if (workerSign_s != null) {
            List<String> workSigns = new LinkedList<>();
            for (String workSign : workerSign_s.split(",")) {
                workSigns.add(workSign);
            }
            confinedSpaceTicketVO.setWorkerSignList(workSigns);
        }

        // 获取enableFieldMap
        Integer currentStep = confinedSpaceTicket.getCurrentStep();
        // 根据大步骤和字段获取当前小步骤列表
        List<Long> subStepList = confinedFlowService.getSubIdsByCurrent(companyId, currentStep);
        // 获取List<map<人员id,List<可编辑字段>>>
        List<Map<List<Long>, List<String>>> enableField = confinedFlowService.getEnableFieldMap(companyId, currentStep, subStepList);

        confinedSpaceTicketVODTO.setConfinedSpaceTicketVO(confinedSpaceTicketVO);
        confinedSpaceTicketVODTO.setEnableFieldMap(enableField);

        return confinedSpaceTicketVODTO;
    }


    /**
     * 列表查找
     *
     * @param confinedSpaceTicketQO
     * @return
     */
    public IPage<ConfinedSpaceTicketVO> getListByQO(ConfinedSpaceTicketQO confinedSpaceTicketQO) {
        Page<ConfinedSpaceTicketVO> confinedSpaceTicketPage = new Page<>(confinedSpaceTicketQO.getCurrent(), confinedSpaceTicketQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        confinedSpaceTicketPage.getOrders().add(orderItem);
        IPage<ConfinedSpaceTicketVO> res = confinedSpaceMapper.getListByQO(confinedSpaceTicketPage, confinedSpaceTicketQO);
        return res;
    }

    /**
     * 设置小步骤的isCommit**字段为1，表示已提交
     *
     * @param subStep
     * @param confinedSpaceTicket
     * @return
     */
    public ConfinedSpaceTicket setSubStepFinish(Integer subStep, ConfinedSpaceTicket confinedSpaceTicket) {
        switch (subStep) {
            case 1: confinedSpaceTicket.setIsCommit1(1); break;
            case 2: confinedSpaceTicket.setIsCommit2(1); break;
            case 3: confinedSpaceTicket.setIsCommit3(1); break;
            case 4: confinedSpaceTicket.setIsCommit4(1); break;
            case 5: confinedSpaceTicket.setIsCommit5(1); break;
            case 6: confinedSpaceTicket.setIsCommit6(1); break;
            case 7: confinedSpaceTicket.setIsCommit7(1); break;
            case 8: confinedSpaceTicket.setIsCommit8(1); break;
            case 9: confinedSpaceTicket.setIsCommit9(1); break;
            case 10: confinedSpaceTicket.setIsCommit10(1); break;
            case 11: confinedSpaceTicket.setIsCommit11(1); break;
            case 12: confinedSpaceTicket.setIsCommit12(1); break;
        }
        return confinedSpaceTicket;
    }
}

